package com.xnew.resource.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.exception.CustomException;
import com.xnew.resource.bean.AuResource;
import com.xnew.resource.service.AuResourceService;
import com.xnew.utils.RedisUtil;
import com.xnew.utils.ResponseUtil;
import com.xnew.utils.StringUtils;
import com.xnew.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sysResource_api")
public class AuResourceController {
	
	@Autowired
	private AuResourceService resourceService;
	
	@ResponseBody
	@RequestMapping("/getSysResourceList")
	public String getSysResourceList(HttpServletRequest request){
		try{
			List<AuResource> sysMenuVoList = resourceService.createResourceTree();
			return ResponseUtil.responseMsg(sysMenuVoList);
		}catch(Exception e){
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	@ResponseBody
	@RequestMapping("/addOrUpdateResource")
	public String addOrUpdateResource(@RequestBody JSONObject json){
		try{
			resourceService.addOrUpdateResource(JSONObject.toJavaObject(json, AuResource.class));
			return ResponseUtil.responseMsg("0001", "操作成功");
		}catch(Exception e){
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "操作失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/queryResourceById")
	public String queryResourceById(@RequestBody JSONObject json){
		try{
			AuResource resource = resourceService.queryResourceById(json.getString("id"));
			return ResponseUtil.responseMsg(resource);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "操作失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteById")
	public String deleteById(@RequestBody JSONObject json){
		try{
			resourceService.deleteById(json.getString("id"));
			return ResponseUtil.responseMsg("0001", "操作成功");
		}catch(Exception e){
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "操作失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/selectRoleResource")
	public String selectRoleResource(@RequestBody JSONObject json) throws Exception{
		try {
			String roleId = json.getString("roleId");
			if(StringUtils.isBlank(roleId)) {
				return ResponseUtil.responseMsg("9997", "角色ID不能为空");
			}
			List<AuResource> list = resourceService.selectRoleResource(roleId);
			return ResponseUtil.responseMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	/**
	 * @方法名：getRoleResourceList
	 * @功能说明：角色资源树
	 * @author yangyuan3
	 * @date  2019年9月10日 下午5:15:56
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRoleResourceList")
	public String getRoleResourceList(HttpServletRequest request) {
		try {
//			SysUserInfo user = (SysUserInfo) request.getSession().getAttribute(Constant.LOGIN_USER);
			SysUserInfo user = UserUtil.getUserInfo(request);
			String roleCd = RedisUtil.get("USER_ROLE:" + user.getUserInfoNo());
//			if(roleCd.contains("R100000") || roleCd.contains("R100060")) {
				List<AuResource> sysMenuVoList = resourceService.createResourceTree();
				return ResponseUtil.responseMsg(sysMenuVoList);
//			} else {
//				List<AuResource> roleMenuList = resourceService.createRoleResourceTree(user);
//				return ResponseUtil.responseMsg(roleMenuList);
//			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	/**
	 * @方法名：selectAllotRoleResource
	 * @功能说明：反显已分配角色资源
	 * @author yangyuan3
	 * @date  2019年9月10日 下午5:36:47
	 * @param json
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectAllotRoleResource")
	public String selectAllotRoleResource(@RequestBody JSONObject json) throws Exception {
		try {
			String roleId = json.getString("roleId");
			if(StringUtils.isBlank(roleId)) {
				return ResponseUtil.responseMsg("9997", "角色ID不能为空");
			}
			List<AuResource> list = resourceService.selectAllotRoleResource(roleId);
			return ResponseUtil.responseMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}

	/**
	 * @方法名：insertRoleResource
	 * @功能说明：分配角色资源
	 * @author yangyuan3
	 * @date  2019年9月10日 下午2:58:37
	 * @param json
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insertRoleResource")
	public String insertRoleResource(@RequestBody JSONObject json, HttpServletRequest request) {
		try {
//			UserUtil.getUserInfo(request);
			SysUserInfo user = UserUtil.getUserInfo(request);;

			String roleId = json.getString("roleId");
			if(StringUtils.isBlank(roleId)) {
				return ResponseUtil.responseMsg("9997", "角色ID不能为空");
			}
			JSONArray visibleResourceIds = json.getJSONArray("visibleResourceIds");
			JSONArray useableResourceIds = json.getJSONArray("useableResourceIds");
			resourceService.insertRoleResource(roleId, visibleResourceIds, user.getUserInfoNo());
			return ResponseUtil.responseMsg("0001", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "操作失败");
		}
	}
}
